/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.stateless;

import entity.Customer;
import entity.Inquiry;
import entity.InquiryItem;
import entity.Lead;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Wx
 */
@Stateless
public class SalesInquirySessionBean implements SalesInquirySessionBeanLocal, SalesInquirySessionBeanRemote {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void updateInquiryItems(List<InquiryItem> inquiryItems) {
        for (Object o : inquiryItems) {
            InquiryItem temp = (InquiryItem) o;
            InquiryItem ii = getInquiryItem(temp.getId());

            ii.setDiscount(round(temp.getDiscount(), 2));
            ii.setQuantity(temp.getQuantity());
            ii.setUnitPrice(round(temp.getUnitPrice(), 2));

            entityManager.persist(ii);
        }
    }

    private double round(double valueToRound, int numberOfDecimalPlaces) {
        double multiplicationFactor = Math.pow(10, numberOfDecimalPlaces);
        double interestedInZeroDPs = valueToRound * multiplicationFactor;

        return Math.round(interestedInZeroDPs) / multiplicationFactor;
    }

    @Override
    public void updateLead(Long id, Inquiry i) {
        try {
            Lead l = entityManager.find(Lead.class, id);

            List<Inquiry> inquiries = l.getInquiries();

            inquiries.add(i);

            l.setInquiries(inquiries);

            entityManager.persist(l);
            entityManager.flush();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void updateCustomer(Long id, Inquiry i) {
        try {
            Customer c = entityManager.find(Customer.class, id);

            List<Inquiry> inquiries = c.getInquiries();

            inquiries.add(i);

            c.setInquiries(inquiries);

            entityManager.persist(c);
            entityManager.flush();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void updateInquiry(Long id, InquiryItem ii) {
        try {
            Inquiry i = entityManager.find(Inquiry.class, id);
            List<InquiryItem> inquiryItems = i.getInquiryItems();
            inquiryItems.add(ii);
            i.setInquiryItems(inquiryItems);
            entityManager.persist(i);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void updateInquiry(Long id, String status) {
        try {
            Inquiry i = entityManager.find(Inquiry.class, id);

            i.setStatus((status));

            entityManager.persist(i);
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }

    @Override
    public Inquiry getInquiry(Long id) {
        Inquiry i = entityManager.find(Inquiry.class, id);

        return i;
    }

    @Override
    public InquiryItem getInquiryItem(Long id) {
        InquiryItem ii = entityManager.find(InquiryItem.class, id);

        return ii;
    }

    @Override
    public void deleteInquiryItem(Long inquiryId, Long inquiryItemId) {
        Inquiry temp = getInquiry(inquiryId);
        InquiryItem temp2 = entityManager.find(InquiryItem.class, inquiryItemId);

        temp.getInquiryItems().remove(temp2);

        entityManager.persist(temp);

        entityManager.remove(temp2);
    }
    
        @Override
    public void deleteInquiry(Long inquiryId){
        
        Inquiry deleteInquiry = entityManager.find(Inquiry.class, inquiryId);
        
        for(InquiryItem inquiryitem: deleteInquiry.getInquiryItems()){
            entityManager.remove(inquiryitem);
        }
       
        deleteInquiry.getInquiryItems().clear();
        entityManager.remove(deleteInquiry);
       
    }
    

//    
//    @Override
//    public <List>Inquiry getUnconvertedInquiries(Long id) {
//        Query query = entityManager.createQuery("SELECT i FROM Customer u");
//        ArrayList<Customer> al = new ArrayList<Customer>();
//        for (Object o : query.getResultList()) {
//            Customer l = (Customer) o;
//            al.add(l);
//        }
//        return al;
//    }
}
